/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.ui.customize;

import java.awt.Image;
import java.awt.Insets;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import lineage.ui.L1Frame;
import lineage.ui.util.L1ChangeImage;
import lineage.ui.util.L1LoadImage;
import lineage.util.Util;

import com.sun.awt.AWTUtilities;

/**
 * 自定义不规则窗体-外形根据图片自适应.
 * 
 * @version 2014年2月14日上午10:47:54
 * @author jrwz
 */
public class L1CustomizeFrame extends L1Frame {
    private static final long serialVersionUID = 7251894496005265955L;
    protected int tempX;
    protected int tempY;
    /** 是否正在拖拽. */
    protected boolean isDragging = false;
    /** 窗体外形图片. */
    private Image img;
    /** 窗体外形图片路径(图片必须是png格式). */
    protected String imgURL;
    private String frameImgURL;
    /** 窗体的不透明度-[范围0.0~1.0]-默认1.0(不透明). */
    protected float visibility = 1.0f;
    /** 跟主窗体外形一样的面板. */
    private NL1CustomizePanel nlcp;
    /** 窗体的关闭按钮. */
    private NButtonClosed nbc;
    /** 窗体的最小化按钮. */
    private NButtonMinimize nbm;

    protected L1CustomizeFrame() {
    }

    @Override
    protected void init(boolean flag) {
        // L1LoadImage.getInstance().load(imgURL);
        img = L1LoadImage.getInstance().getImg(imgURL);
        frameImgURL = imgURL;
        if (img == null) {
            Util.message("没有找到图片(必须是png图片)：" + "(" + imgURL + ")");
            return;
        }
        MediaTracker mt = new MediaTracker(this);
        mt.addImage(img, 0);
        try {
            mt.waitForAll(); // 开始加载由此媒体跟踪器跟踪的所有图像。此方法将等待，直到所有被跟踪的图像已经完成加载。
        } catch (InterruptedException e) {
            Util.message(e.getLocalizedMessage());
        }

        _width = img.getWidth(null);
        _height = img.getHeight(null);

        setUndecorated(true); // 设置无边框
        super.init(flag);
    }

    /**
     * 在此增加组件必须用 getPanel().add() 方法.
     */
    @Override
    protected void add() {
        // 圆角窗体
        // AWTUtilities.setWindowShape(this, new RoundRectangle2D.Double(0.0D, 0.0D, getWidth(), getHeight(), 26.0D, 26.0D));
        // 将窗体设置为指定的Shape形状
        AWTUtilities.setWindowShape(this, L1ChangeImage.getImageShape(img));
        // 设置窗体可见度
        // AWTUtilities.setWindowOpacity(this, visibility);
        setOpacity(visibility); // 设置初始不透明度
        nlcp = new NL1CustomizePanel(true);
        add(nlcp); // 增加面板(在窗体上加组件实际上是在面板上增加)

        // 增加按钮
        nbc = new NButtonClosed();
        nbm = new NButtonMinimize();
        getPanel().add(nbc); // 关闭按钮
        getPanel().add(nbm); // 最小化按钮
    }

    @Override
    protected void addListener() {
        super.addListener();
        addMouseListener(this); // 增加鼠标监听器
        addMouseMotionListener(this); // 增加鼠标移动监听器
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // 鼠标左键16/鼠标右键4
        if (e.getModifiers() == 16) {
            tempX = e.getX();
            tempY = e.getY();
            isDragging = true;
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        isDragging = false;
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (isDragging) {
            int left = getLocation().x;
            int top = getLocation().y;
            setLocation(left + e.getX() - tempX, top + e.getY() - tempY);
        }
    }

    /**
     * 取得基础面板,在窗体上加组件实际是在这个面板上加.
     * 
     * @return 跟主窗体外形一样的面板
     */
    protected NL1CustomizePanel getPanel() {
        return nlcp;
    }

    /**
     * 取得关闭按钮.
     * 
     * @return 关闭按钮
     */
    protected NButtonClosed getButtonClosed() {
        return nbc;
    }

    /**
     * 取得最小化按钮.
     * 
     * @return 最小化按钮
     */
    protected NButtonMinimize getButtonMinimize() {
        return nbm;
    }

    /**
     * 取得该类的实例.
     * 
     * @return 类的实例
     */
    private JFrame get() {
        return this;
    }

    /**
     * 跟主窗体外形一样的面板.
     */
    protected class NL1CustomizePanel extends L1CustomizePanel {
        private static final long serialVersionUID = -108224394978790412L;

        protected NL1CustomizePanel(boolean flag) {
            // 一般不要设置
            _x = 0;
            _y = 0;
            imgURL = frameImgURL;
            // end
            init(flag);
        }
    }

    /**
     * 内部类：关闭按钮.
     */
    protected final class NButtonClosed extends L1CustomizeButton {
        private static final long serialVersionUID = -5650062814416770387L;

        private NButtonClosed() {
            // 要设置的部分
            _x = get().getWidth() - 50;
            _y = 0;
            toolTipText = "关闭";
            imgURL = "./image/button/24.png";
            imgOverURL = "./image/button/25.png";
            imgPressedURL = "./image/button/26.png";
            imgDisabledURL = "./image/button/27.png";
            // end

            insets = new Insets(0, 0, 0, 0);

            init(true);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getModifiers() == MouseEvent.BUTTON1_MASK) { // 鼠标左键单击
                get().dispose(); // 释放此窗口资源
            }
        }
    }

    /**
     * 内部类：最小化按钮.
     */
    protected final class NButtonMinimize extends L1CustomizeButton {
        private static final long serialVersionUID = -646735185085493629L;

        private NButtonMinimize() {
            // 要设置的部分
            _x = get().getWidth() - 76;
            _y = 0;
            toolTipText = "最小化";
            imgURL = "./image/button/28.png";
            imgOverURL = "./image/button/29.png";
            imgPressedURL = "./image/button/30.png";
            imgDisabledURL = "./image/button/31.png";
            // end

            insets = new Insets(0, 0, 0, 0);

            init(true);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getModifiers() == MouseEvent.BUTTON1_MASK) { // 鼠标左键单击
                get().setExtendedState(JFrame.ICONIFIED);
            }
        }
    }
}